<?php

class Admin_m extends CI_Model{
	
	
	function add(){
	
	}
	function edit(){
		
		// uploading the banner ads file.. 
		$fileext = $_FILES['profile_pic']['name'];
		$ext = substr(strrchr($fileext, '.'), 1); 
		$filename = $this->input->post('username').'_pic.'.$ext;
		
		$config['upload_path'] = './images/admin';
		$config['allowed_types'] = 'jpg|png';
		$config['max_size']	= '5000';
		$config['max_width']  = '215';
		$config['max_height']  = '215';
		$config['file_name'] = $filename;
		
		$this->upload->initialize($config);
		// loading the configuration.. 

		if ( ! $this->upload->do_upload('profile_pic'))
		{
			$error = array('error' => $this->upload->display_errors());
			// check the database if profile pic has a value or not..
			$this->db->where('id',$this->session->userdata('admin_id'));
			$res = $this->db->get('admin')->row();
			if($res->profile_pic == ""){
				$filename = 'default.png';
			}
			else{
				$filename = $res->profile_pic;
			}
		}
		if($this->input->post('password') != ''){
			$data = array(
				'fname' => $this->input->post('fname'),
				'password' => $this->input->post('password'),
				'lname' => $this->input->post('lname'),
				'profile_pic' => $filename,
				'name_caption' => $this->input->post('name_caption'),
				'position' => $this->input->post('position'),
			);
		}
		else{
			$data = array(
				'fname' => $this->input->post('fname'),
				'lname' => $this->input->post('lname'),
				'profile_pic' => $filename,
				'name_caption' => $this->input->post('name_caption'),
				'position' => $this->input->post('position'),
			);
		}

			$this->db->where('id',$this->session->userdata('admin_id'));
			$res = $this->db->update('admin',$data);
			return ($res) ? true:false;
	}
	function delete(){
	
	}
	function get_admin_info($id){
		
		$this->db->where('id',$id);
		$res = $this->db->get('admin');
		
		return $res->row();
		
	}
	function admin_auth(){
		$q = "select * from admin where BINARY username = '".$this->input->post('username')."' and BINARY password = '".$this->input->post('password')."'";
		
		$res = $this->db->query($q);
		
		if($res->num_rows() > 0){
			return $res->row();
		}
		else{
			return false;
		}
	}
}// end class admin model..